define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Show, GoPage, GoAddress, $window, $timeout, $ionicSlideBoxDelegate,$ionicScrollDelegate) {
		$scope.input = new Object();
		$scope.input.region = ''; //默认全部
		$scope.input.key = "";
		$scope.index = 0; //默认显示第一个市场下的店铺，指明是哪个市场
		$scope.currentMarket = new Object(); //选中的店铺，默认显示第一个
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			//动态设置高度
			var lan = 0; //这是状态栏，只有ios的APP才有
			var all = $window.innerHeight;
			if(window.cordova && window.cordova.plugins) {
				if(ionic.Platform.isIOS()) {
					lan = 20;
				}
			}
			$scope.myObj = {
				"height": all - 44 - 44 - 0 - lan + "px"
			};
		}, false);


		//滑动时候触发
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};

		//展会
		Show.getlist($scope.input.key, 10, 1, function(data) {
			$scope.data = data.exhibition;
		});
		//数据加载
		$scope.init_data = function() { //为了重用该方法，把展会的初始化方法移出去
			//行业分布
			Show.distribution($scope.input.region, 10, 1, function(data) {
				$scope.distribution = data.market;
				$scope.region = data.region;
				$scope.divisionShop($scope.index);
			});
		}
		$scope.init_data();
		$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);

		$scope.gold = new Array(); //存放金牌店铺
		//$scope.nogold = new Array();//非金牌店铺。市场店铺是指所有店铺，包括金牌店铺
		$scope.divisionShop = function(index) { //循环找出金牌店铺和非金牌店铺
			$scope.gold.splice(0, $scope.gold.length); //必须清空原有数据，否则会重复
			$scope.currentMarket = $scope.distribution[index];
			if($scope.currentMarket != undefined)
				$scope.shop = $scope.currentMarket.shop;
			if($scope.shop != undefined || $scope.shop != null) {
				for(var i = 0; i < $scope.shop.length; i++) {
					if($scope.shop[i].is_gold == 1) {
						$scope.gold.push($scope.shop[i]);
					}
				}
			}
		}

		$scope.showmarket = function(index) { //选择市场，显示该市场下对应的店铺
			$scope.index = index;
			$scope.divisionShop($scope.index);
		}

		//跳转到外部的第三方链接
		$scope.onshow_outLink = function(url) {
			GoPage.go_link(url);
		}

		//根据地区查找店铺
		$scope.get_region = function(id) {
			$scope.input.region = id; //控制样式
			$scope.index = 0; //默认第1个市场
			$scope.nPage_d = 1; //必须初始化页数，为了给当前条件上下拉刷新用
			$scope.init_data();
		}

		/*===========下拉刷新 =============*/
		$scope.nPage_show = 1;
		$scope.nPage_d = 1;
		$scope.isLoad = false;
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			if($scope.selectTab == 0) {
				Show.getlist($scope.input.key, 10, 1, function(data) {
						if(data != null) {
							$scope.data = data.exhibition;
						} else {
							$scope.data = new Object();
						}
						$scope.nPage_show = 1;
					},
					function() {
						$scope.$broadcast('scroll.refreshComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			}
			if($scope.selectTab == 1) {
				Show.distribution($scope.input.region, 10, 1, function(data) {
					if(data != null) {
						$scope.index = 0;
						$scope.distribution = data.market;
						$scope.region = data.region;
						$scope.divisionShop($scope.index);
					} else {
						$scope.distribution = new Array();
					}
					$scope.nPage_d = 1;
				}, function() {
					$scope.$broadcast('scroll.refreshComplete');
					$timeout(function() {
						$scope.isLoad = false;
					}, 1500);
				});
			}
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.selectTab == 0) {
				Show.getlist($scope.input.key, 10, $scope.nPage_show + 1, function(data) {
						if(data != null) {
							if($scope.data != undefined) {
								$scope.data = $scope.data.concat(data.exhibition);
//								console.log($scope.data)
								$scope.nPage_show += 1;
							} else {
								$scope.data = data.exhibition;
							}
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			}
			if($scope.selectTab == 1) { //$scope.distribution = data.market;
				Show.distribution($scope.input.region, 10, $scope.nPage_d + 1, function(data) {
						if(data != null) {
							if($scope.distribution != undefined) {
								$scope.distribution = $scope.distribution.concat(data.market);
								$scope.nPage_d += 1;
							} else {
								$scope.distribution = data.market;
							}
							$scope.divisionShop($scope.index);
						}
					},
					function() {
						$scope.$broadcast('scroll.infiniteScrollComplete');
						$timeout(function() {
							$scope.isLoad = false;
						}, 1500);
					});
			}
		};
		/*===========下拉刷新 =============*/

		//行业展会的查询
		$scope.doSearch = function() {
			Show.getlist($scope.input.key, 10, 1, function(data) {
				$scope.data = data.exhibition;
			});
		};
		//行业展会-详情
		$scope.onshow_detail = function(id) {
			GoPage.go_states('showdetail', { "id": id });
		};
		//跳转到一览
		$scope.onshow_detail_lan = function(type) {
			GoPage.go_states("industryrankingsdetail",{"store_type":type});
		};
		//地区的显示更多
		$scope.isMore_region = true; //默认隐藏
		$scope.isMore_region_button = "更多";
		$scope.showMore_region = function() {
			$scope.isMore_region = !$scope.isMore_region;
			$scope.isMore_region_button = $scope.isMore_region ? "更多" : "收起";
		}
		
	}

	controller.$inject = ['$scope', 'Show', 'GoPage', 'GoAddress', '$window', '$timeout', '$ionicSlideBoxDelegate','$ionicScrollDelegate'];

	return app.registerController("showctrl", controller);
});